﻿////////////// ERRORS

var g_Errors = new Array();

function showError() {

    $.each(g_Errors, function (idx, v) {

        var message = $("<h4 class=\"alert_error\"></h4>").text(v)
                                                          .delay(5000)
                                                          .fadeOut("slow");

        $("#infoMessage").append(message);
    });
}

function hideError() {
}

function clearErrors() {

    g_Errors = new Array();
    $("#infoMessage").html("");
}

function addError(error) {

    g_Errors.push(error);
}

function showErrors() {

    var hasErrors = g_Errors.length > 0;

    if (hasErrors) {

        showError();
    }
    else {

        hideError();
    }

    return hasErrors;
}

function displayErrorMessage(message) {

    clearErrors();

    addError(message);

    showErrors();
}

////////////// SUCCESS

var g_SuccessMessages = new Array();

function showSuccessMessage() {

    $.each(g_SuccessMessages, function (idx, v) {

        var message = $("<h4 class=\"alert_success\"></h4>").text(v)
                                                          .delay(5000)
                                                          .fadeOut("slow");

        $("#infoMessage").append(message);
    });
}

function hideSuccessMessage() {
}

function clearSuccessMessages() {

    g_SuccessMessages = new Array();
    $("#infoMessage").html("");
}

function addSuccessMessage(message) {

    g_SuccessMessages.push(message);
}

function showSuccessMessages() {

    var hasSuccessMessages = g_SuccessMessages.length > 0;

    if (hasSuccessMessages) {

        showSuccessMessage();
    }
    else {

        hideSuccessMessage();
    }

    return hasSuccessMessages;
}

function displaySuccessMessage(message) {

    clearSuccessMessages();

    addSuccessMessage(message);

    showSuccessMessages();
}

function ajaxRequest(url, params, onsuccess, onerror, method) {

    method = method || "GET";

    $.ajax({

        url: url,
        cache: false,
        data: params,
        dataType: "json",
        type: method,
        success: function (data, textStatus, jqXHR) {

            var error = data.hasError;

            if (error) {

                clearErrors();
                addError(data.errorMessage);
                showErrors();

                onerror(data.errorMessage);
            }
            else {

                onsuccess(data.data);
            }
        },
        error: function (jqXHR, textStatus, errorThrown) {

            onerror(errorThrown);
        }
    });
}